package cn.jzk.mySpringSecurity_noSeparation.Controller;

import cn.jzk.mySpringSecurity_noSeparation.Entity.User;
import cn.jzk.mySpringSecurity_noSeparation.Service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jiangzk
 * 2025/1/17  上午10:09
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PreAuthorize("hasAuthority('USER:ADD')")
    @Operation(summary = "添加用户信息")
    @PostMapping("/add")
    public int add(@RequestBody User user) {
        return userService.add(user);
    }

    @PreAuthorize("hasAuthority('USER:UPDATE')")
    @Operation(summary = "修改用户信息")
    @PostMapping("/update")
    public int update(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @PreAuthorize("hasAuthority('USER:DELETE')")
    @Operation(summary = "删除用户信息")
    @GetMapping("/delete")
    public int delete(Integer id) {
        return userService.deleteUser(id);
    }

    @Operation(summary = "基于 security 上下文对象，获取当前认证对象的信息（只获取用户登录时的信息）")
    @GetMapping("userInfo")
    public Map<String,Object> getUserInfo() {
        return userService.getUserInfo();
    }

    @PreAuthorize("hasAuthority('USER:LIST')")
    @Operation(summary = "查询用户列表")
    @GetMapping("/list")
    public List<User> list() {
        return userService.listAll();
    }
}
